package com.xmindguoguo.boot.core.log.factory;

import java.util.Objects;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xmindguoguo.boot.modular.system.model.TSystemErrorLogModel;
import com.xmindguoguo.boot.modular.system.model.TSystemLoginLogModel;
import com.xmindguoguo.boot.modular.system.model.TSystemMenuModel;
import com.xmindguoguo.boot.modular.system.model.TSystemOperationLogModel;
import com.xmindguoguo.boot.modular.system.service.ITSystemErrorLogService;
import com.xmindguoguo.boot.modular.system.service.ITSystemLoginLogService;
import com.xmindguoguo.boot.modular.system.service.ITSystemMenuService;
import com.xmindguoguo.boot.modular.system.service.ITSystemOperationLogService;

import lombok.extern.slf4j.Slf4j;

/**
 * 日志操作任务创建工厂
 *
 * @author fengshuonan
 * @date 2016年12月6日 下午9:18:27
 */
@Slf4j
@Component
public class LogTaskFactory {
    public static ITSystemLoginLogService systemLoginLogService;
    public static ITSystemErrorLogService systemErrorLogService;
    public static ITSystemMenuService systemMenuService;
    public static ITSystemOperationLogService systemOperationLogService;

    @Autowired
    public void setSystemOperationLogService(ITSystemOperationLogService systemOperationLogService) {
        LogTaskFactory.systemOperationLogService = systemOperationLogService;
    }

    @Autowired
    public void setSystemLoginLogService(ITSystemLoginLogService systemLoginLogService) {
        LogTaskFactory.systemLoginLogService = systemLoginLogService;
    }

    @Autowired
    public void setSystemMenuService(ITSystemMenuService systemMenuService) {
        LogTaskFactory.systemMenuService = systemMenuService;
    }

    @Autowired
    public void setSystemErrorLogService(ITSystemErrorLogService systemErrorLogService) {
        LogTaskFactory.systemErrorLogService = systemErrorLogService;
    }

    public static TimerTask errorLog(TSystemErrorLogModel errorLogModel) {
        return new TimerTask() {
            public void run() {
                try {
                    Wrapper<TSystemMenuModel> wrapper = new EntityWrapper<>();
                    wrapper.setSqlSelect("name");
                    wrapper.eq("reqUrl", errorLogModel.getReqUrl());
                    TSystemMenuModel systemMenuModel = systemMenuService.selectOne(wrapper);
                    if (Objects.nonNull(systemMenuModel)) {
                        errorLogModel.setReqName(systemMenuModel.getName());
                    }
                    systemErrorLogService.insert(errorLogModel);
                } catch (Exception e) {
                    log.error("创建登录日志异常!", e);
                }
            }
        };
    }

    public static TimerTask loginLog(String ip, String userAgentStr, String username, String password, String msg) {
        return new TimerTask() {
            public void run() {
                try {
                    TSystemLoginLogModel loginLog = LogModelFactory.getTSystemLoginLogModel(ip, userAgentStr, username, password, msg);
                    systemLoginLogService.insert(loginLog);
                } catch (Exception e) {
                    log.error("创建登录日志异常!", e);
                }
            }
        };
    }

    public static TimerTask operationLog(TSystemOperationLogModel model) {
        return new TimerTask() {
            public void run() {
                try {
                    systemOperationLogService.insert(model);
                } catch (Exception e) {
                    log.error("创建操作日志异常!", e);
                }
            }
        };
    }
}
